/*
 * Copyright 2016 balena.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Dictionary } from 'lodash';
import { outdent } from 'outdent';
import * as prettyBytes from 'pretty-bytes';

export const progress: Dictionary<(quantity: number) => string> = {
	successful: (quantity: number) => {
		const plural = quantity === 1 ? '' : '';
		return `${plural}个设备刷写成功`;
	},

	failed: (quantity: number) => {
		const plural = quantity === 1 ? '' : '';
		return `${plural}个设备刷写失败`;
	},
};

export const info = {
	flashComplete: (
		imageBasename: string,
		[drive]: [{ description: string; displayName: string }],
		{ failed, successful }: { failed: number; successful: number },
	) => {
		const targets = [];
		if (failed + successful === 1) {
			targets.push(`刷写${drive.description} (${drive.displayName})`);
		} else {
			if (successful) {
				const plural = successful === 1 ? '' : '';
				targets.push(`刷写${plural}个目标 ${successful}`);
			}
			if (failed) {
				const plural = failed === 1 ? '' : '';
				targets.push(`未能刷写${plural}个目标 ${failed}`);
			}
		}
		return `${imageBasename} 已成功刷写 ${targets.join(' ')}`;
	},
};

export const compatibility = {
	sizeNotRecommended: () => {
		return '不建议';
	},

	tooSmall: () => {
		return '磁盘太小';
	},

	locked: () => {
		return '已锁定';
	},

	system: () => {
		return '系统驱动器';
	},

	containsImage: () => {
		return '源驱动器';
	},

	// The drive is large and therefore likely not a medium you want to write to.
	largeDrive: () => {
		return '大容量驱动器';
	},
} as const;

export const warning = {
	tooSmall: (source: { size: number }, target: { size: number }) => {
		return outdent({ newline: ' ' })`
			选定的源是 ${prettyBytes(source.size - target.size)}
			比这个驱动器大.
		`;
	},

	exitWhileFlashing: () => {
		return [
			'您当前正在刷写驱动器。',
			'关闭Etcher可能会使驱动器无法使用。',
		].join(' ');
	},

	looksLikeWindowsImage: () => {
		return [
			'看起来您正在尝试刷写Windows映像。\n\n',
			'与其他映像不同，Windows映像需要特殊处理才能启动。',
			'我们建议您使用专门为此目的设计的工具，例如',
			'<a href="https://rufus.akeo.ie">Rufus</a> (Windows),',
			'<a href="https://github.com/slacka/WoeUSB">WoeUSB</a> (Linux),',
			'或MACOS Boot Camp启动转换助理。',
		].join(' ');
	},

	missingPartitionTable: () => {
		return [
			'看来这不是可引导映像。\n\n',
			'该映像似乎不包含分区表,',
			'可能无法被您的设备识别或引导。',
		].join(' ');
	},

	driveMissingPartitionTable: () => {
		return outdent({ newline: ' ' })`
			看来这不是可引导驱动器。
			该驱动器似乎不包含分区表,
			并且可能无法被您的设备识别或引导.
		`;
	},

	largeDriveSize: () => {
		return '这是一个大容量驱动器! 请确保它不包含要保留的文件！';
	},

	systemDrive: () => {
		return '选择系统驱动器很危险，这样会擦除你的驱动器!';
	},

	sourceDrive: () => {
		return '包含您选择要刷写的镜像';
	},
};

export const error = {
	notEnoughSpaceInDrive: () => {
		return [
			'驱动器上没有足够的空间。',
			'请插入较大容量的驱动器，然后重试。',
		].join(' ');
	},

	genericFlashError: (err: Error) => {
		return `出问题了。如果是压缩映像，请检查档案是否未损坏。\n${err.message}`;
	},

	validation: () => {
		return [
			'写入已成功完成，但Etcher检测到了潜在的问题',
			'从驱动器读回映像时出现损坏问题。',
			'\n\n请考虑将映像写入其他驱动器。',
		].join(' ');
	},

	openSource: (sourceName: string, errorMessage: string) => {
		return outdent`
			打开${sourceName}时出了点问题

			错误: ${errorMessage}
		`;
	},

	flashFailure: (
		imageBasename: string,
		drives: Array<{ description: string; displayName: string }>,
	) => {
		const target =
			drives.length === 1
				? `${drives[0].description} (${drives[0].displayName})`
				: `${drives.length}个目标`;
		return `将${imageBasename}写入到${target}出了点问题。`;
	},

	driveUnplugged: () => {
		return [
			'看起来Etcher无法访问驱动器。',
			'它是否被意外拔出？',
			"\n\n有时，此错误是由无法提供对驱动器的稳定访问的故障读取器引起的。",
		].join(' ');
	},

	inputOutput: () => {
		return [
			'看来Etcher无法写入驱动器的此位置。',
			'此错误通常是由驱动器，读取器或端口故障引起的。',
			'\n\n请使用其他驱动器，读取器或端口再试一次。',
		].join(' ');
	},

	childWriterDied: () => {
		return [
			'刷写进程意外结束。',
			'如果问题仍然存在，请重试，并与Etcher团队联系。',
		].join(' ');
	},

	unsupportedProtocol: () => {
		return '仅支持http://和https:// URL格式。';
	},
};
